<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>	
<%@ include file="/pub/headmeta.jsp" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
</head>
<body class="edit-bgr-color">
	<div class="right-head"><span class="right-head-font">修改商品</span></div>
	<div align="center" style="width: 98%">	
		<form id="editForm" method="post" enctype="multipart/form-data">
		<input type="hidden" name="goodsId" id="goodsId" value="${data.goodsId}"/>			
			<table cellpadding="5" style="width: 600px; font-size: 12px;">
				<tr>
					<td width="35%" align="right">淘宝客url：</td>
					<td width="65%" align="left"><input type="text" name="link" id="link" style="width: 80%" value="${data.link}"/>&nbsp;&nbsp;<font color='red'>*</font></td>					
				</tr>
				<tr>
					<td align="right">商品名称：</td>
					<td align="left"><input type="text" name="title" id="title" style="width: 80%" value="${data.title}"/>&nbsp;&nbsp;<font color='red'>*</font></td>					
				</tr>
				<tr>
					<td align="right">所属分类：</td>
					<td align="left"><select name="goodsTypeId" id="goodsTypeId" style="width: 80%"/>&nbsp;&nbsp;<font color='red'>*</font></td>					
				</tr>
				<tr>
					<td align="right">适合性格：</td>
					<td align="left"><table id='kidArea' style="width: 80%"></table></td>					
				</tr>
				<tr>
					<td align="right">适合体型：</td>
					<td align="left"><table id='bodyArea' style="width: 80%"></table></td>					
				</tr>
				<tr>
					<td align="right">购买建议：</td>
					<td align="left"><textarea name="suggest" id="suggest" style="width:80%" rows="5">${data.suggest}</textarea></td>					
				</tr>
				<tr>
					<td align="right">展现图片：</td>
					<td align="left"><input type="file" name="pic" id="pic" style="width: 80%"/>&nbsp;&nbsp;<img height='40' width='40' src='${ctx}/goods!getSou.action?picName=${data.pic}'/></td>					
				</tr>			
			</table>
			<br>			
			<div class="edit-line"></div>
			<input  type="button" class="button-1" value="修改" onclick="doEdit();"/>
			<input  type="button" class="button-1" value="重置" onclick="resetEx(this.form);loadData();"/>						
			<input  type="button" class="button-1" value="返回" onclick="back();"/>
		</form>
	</div>	
</body>
</html>
<script type="text/javascript">
<!--
var rowNum=3;
var types;

//自动调用初始化
$(document).ready(function(){	
	//填充性格
	$.ajax({url:'${ctx}/kid!getList.action?page=1&pageSize=99999', data:'', async:false, type:"post", success:function callback(txt){   
			var json = $.parseJSON(txt);
			if (json.code==0) {	
				var table=document.getElementById('kidArea');
				var row;
				for(var i=0;i<json.info.length;i++){					
					var o=json.info[i];
					if(i%rowNum==0){
						row=document.createElement("tr");
						table.appendChild(row);
					}
					var c=document.createElement("td");	
					c.style.width=100/rowNum+'%';
					c.innerHTML="<input type='checkbox' name='kid' value='"+o.kidId+"'/>"+o.name;					
					row.appendChild(c);
				}
			}else{
				alert('查询失败！');
			}
		}
	});		

	//填充体型	
	$.ajax({url:'${ctx}/body!getList.action?page=1&pageSize=99999', data:'', async:false, type:"post", success:function callback(txt){  
			var json = $.parseJSON(txt);
			if (json.code==0) {	
				var table=document.getElementById('bodyArea');
				var row;
				for(var i=0;i<json.info.length;i++){					
					var o=json.info[i];
					if(i%rowNum==0){
						row=document.createElement("tr");
						table.appendChild(row);
					}
					var c=document.createElement("td");	
					c.style.width=100/rowNum+'%';
					c.innerHTML="<input type='checkbox' name='body' value='"+o.bodyId+"'/>"+o.name;					
					row.appendChild(c);
				}
			}else{
				alert('查询失败！');
			}
		}	
	});
	
	//填充商品类型
	$("#goodsTypeId").append("<option value=''>-请选择-</option>");
	$.ajax({url:'${ctx}/goodsType!getSelectType.action', data:'', async:false, type:"post", success:function callback(txt){ 
			var json = $.parseJSON(txt);
			if (json.code==0) {	
				types=json.info;
				for(var i=0;i<json.info.length;i++){
					var o=json.info[i];
					$("#goodsTypeId").append("<option value='"+o.goodsTypeId+"'>"+o.name+"</option>");		
				}
			}else{
				alert('查询失败！');
			}
		}	
	});	
	
	loadData();
});

//初始化
function loadData(){
	document.getElementById('goodsTypeId').value='${data.goodsTypeId}';	
	var kids=new Array();
	<c:forEach var="o" items="${kidList}">kids.push('${o}');</c:forEach>
	mixCheck('kid',kids);
	var bodys=new Array();
	<c:forEach var="o" items="${bodyList}">bodys.push('${o}');</c:forEach>
	mixCheck('body',bodys);
}

//修改
function doEdit() {	
	if ($("#link").val() == ""){
		alert('淘宝客url不能为空！');
		return;
	}
	if ($("#title").val() == ""){
		alert('商品名称不能为空！');
		return;
	}
	var flag=false;
	var type=document.getElementById('goodsTypeId').value;
	for(var i=0;i<types.length;i++){
		if(types[i].goodsTypeId==type){
			if(types[i].pid!=''){
				flag=true;
			}
		}
	}
	if(!flag){
		alert('请选择至所属分类(非大分类)！');
		return;		
	}
	if(!ifCheck('kid')){
		alert('请选择至少一个适合的性格！');
		return;
	}
	if(!ifCheck('body')){
		alert('请选择至少一个适合的体型！');
		return;
	}
	var form=$("#editForm");
	form.attr("action","${ctx}/goods!edit.action"); 
	form.submit();		
}

//返回
function back(){
	location.href="${ctx}/goods!viewList.action";
}
//-->
</script>
